define(['jquery','api','user','pages/index/model','Vue','text!pages/order/detail.html','util'],function($,api,user,model,Vue,tpl,util){
	var view,
	    vm,
	    step=10,
	    timer1,
	    timer2;
	var controller={
	   init:function (opensn) {
	       var str,
	           data;
	       if(!user.checkLogin())return;
	       if(model.code==null){
	   	   		data=api.getGoods();
	   	   		if(!data||(data.errorcode&&data.errorcode!=0)||data.retcode!='0')return;
	   	   		model.addGoods(data);
	       }
	       api.initFooter(1);
	       util.setTitle('持仓单详情');	       
	       this.bind();
           this.loadOrder(opensn);
           this.setTimer();
	    },
	    bind:function(){
	       $('#pages').append('<div id="orderDetail"></div>');
	       if(vm){
	          vm.show=true;
	       }else{
	          vm=new Vue({
	          	  el:'#orderDetail',
	              data:{
	                 show:true,
	                 cfg:util.cfg.noPercent,
	                 closePrice:'--',
	                 fpl:'--',
	                 order:false,
	                 showClose:false,
	                 showStop:false,
	                 closeOrder:false
	              },
	              filters:{
	                 lowHigh:util.lowHigh
	              },
	              methods:{
	                 close:function(){
		                this.closeOrder=this.order;
		                this.showClose=true;
		              },
		              cancelClose:function(){
		              	this.showClose=false;
		              },
		              setStopPercent:function(){
		              	this.showStop=true;
		              },
		              cancelStop:function(){
		                this.showStop=false;
		              },
		              orderModify:function(){
		              	 var order=this.order,
		              	     slpercent=order.slpercent==0||order.slpercent>100?100:order.slpercent;
		              	     data={
		              	         tppercent:order.tppercent,
		              	         slpercent:slpercent,
		              	         tpprice:order.orderprice*order.tppercent/100,
                                 slprice:order.orderprice*slpercent/100,
		              	         ordersn:order.opensn,
		              	         orderprice:order.orderprice
		              	     };
		              	 this.showStop=false;
		                 api.modifyOrder(data,function(rs){
		                     if(rs.retcode=='0'){
		                         util.msg('修改成功！');
		                     }
		                 });
		              },
		              setTicket:function(num,key){
		              	 var val=+this.order[key];
		                     
				       	 if((val==50&&num>0)||(val==0&&num<0))return;
				       	 this.order[key]=val+num*step;
		              },
		              doClose:function(){
		              	var order=this.closeOrder,
		              	    postData={
							    ordersn:order.opensn,
							    goodscode:order.goodscode,
							    orderamount:+order.openamount
						    };
		              	api.closeOrder(postData,function(){
		              		util.msg('平仓成功！');
		              	    setTimeout(function(){history.go(-1);},500);
		              	});
		              	this.showClose=false;
		              }
	              },
	              template:tpl
	          })
	       }
	    },
	    loadOrder:function(id){
	    	var that=this,
	    	    order;
	       api.order(id,function(rs){
	       		if(rs.retcode==='0'){
	       			if(rs.orders !=''){ // 20161209dyx
	       		       order=rs.orders[0];
	       		       order.slpercent=order.slpercent==100?0:order.slpercent;
	       		       vm.order=order;
	       			}else{
	       			   location.hash='#/order/historyDetail/'+id;
	       			}
	       		}else {
                            util.dealErrorCode(rs.retcode);// 20161209dyx:增加提示信息
                        }
	       })	       	    	   
	    },
	    setTimer:function(){
	    	var that=this,
	    	    opensn;
	    	timer1=setTimeout(function(){
	    	    $.getJSON(util.url('ordersn'),function(rs){
	    	         if(rs.retcode=='0'){
	    	         	 opensn=vm.order.opensn;
	    	         	 if(rs.orders.length&&rs.orders.indexOf(opensn)==-1){
	    	         	    util.msg('订单已平仓');
	    	         	    setTimeout(function(){history.go(-1);},500);
	    	         	 }else{
	    	                that.refreshHq(rs.quotes);
	    	         	 }
	    	         }
	    	         if(vm.show)that.setTimer();
	    	    })
	    	},util.cfg.refreshTime)
	    },
	    refreshHq:function(hq){
	    	var order=vm.order,
	    	    goodscode=order.goodscode,
	    	    goods=model.goods_arr[goodscode],
	    	    mindecimal=goods.mindecimal,
	    	    groupid=goods.groupid,
	    	    close=hq[groupid].close,
	    	    closeFormat=(close/Math.pow(10, mindecimal)).toFixed(mindecimal),
	    	    pt,
	    	    newPrice,
	    	    fpl;
            pt = order.direction == 98 ? goods.sellpt : goods.buypt;
            newprice = +close +(+pt);
            fpl = (order.direction == 98 ? 1 : -1) * (newprice - order.holderprice) * order.openamount * goods.agreeunit / Math.pow(10, mindecimal);
            fpl = fpl.toFixed(2);
	    	vm.closePrice=closeFormat;
	    	vm.fpl=fpl;
	    },
        onRouteChange:function(){
	    	vm.show=false
	    	if(timer1)clearTimeout(timer1);
	    }
	}
    return controller;     
})